package com.se7en.ufutil;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.Html;
import android.widget.Toast;

import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class Dining extends BalloonItemizedOverlay<OverlayItem> {


	private ArrayList<OverlayItem> m_overlays = new ArrayList<OverlayItem>();
	private Context ctx;
	private DiningModel [] diningLocs;
	DataAccess dataAccess;
	MapView mapView;
	static Drawable resMarker, dinMarker, errorMarker;

	//constructor
	public Dining(Drawable defaultMarker, Context context, DataAccess dataAccess, MapView mapView) {
		super(boundCenter(defaultMarker), mapView);
		ctx = context;
		this.dataAccess = dataAccess;
		this.mapView = mapView;
		diningLocs = dataAccess.getAllDining();
		dinMarker = ctx.getResources().getDrawable(R.drawable.dining_marker);
		resMarker = ctx.getResources().getDrawable(R.drawable.restaurant_marker);
		errorMarker = ctx.getResources().getDrawable(R.drawable.error);

		OverlayItem temp;

		if(diningLocs.length > 1) {
			for(DiningModel dine : diningLocs) {
				switch(dine.type) {

				case DINING_HALL:
					temp = new OverlayItem(dine.location, dine.name, Html.fromHtml(dine.times)+"\n\t\t"+dine.days);
					temp.setMarker(dinMarker);
					dinMarker.setBounds(-dinMarker.getIntrinsicWidth()/2, -dinMarker.getIntrinsicHeight(), dinMarker.getIntrinsicWidth() /2, 0);
					break;

				case RESTAURANT:
					temp = new OverlayItem(dine.location, dine.name, Html.fromHtml(dine.times)+"\n\t\t"+dine.days);
					temp.setMarker(resMarker);
					resMarker.setBounds(-resMarker.getIntrinsicWidth()/2, -resMarker.getIntrinsicHeight(), resMarker.getIntrinsicWidth() /2, 0);
					break;

				default:
					//TODO Write Error code
					temp = new OverlayItem(dine.location, "ERROR", "Error details...");
					temp.setMarker(errorMarker);
					errorMarker.setBounds(-errorMarker.getIntrinsicWidth()/2, -errorMarker.getIntrinsicHeight(), errorMarker.getIntrinsicWidth() /2, 0);
					break;
				}

				this.addOverlay(temp);
			}
		}
	}


	public void addOverlay(OverlayItem overlay) {
		m_overlays.add(overlay);
		populate();
	}


	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow)
	{
		super.draw(canvas, mapView, shadow);
		return;
	}

	@Override
	protected OverlayItem createItem(int i) {
		return m_overlays.get(i);
	}

	@Override
	public int size() {
		return m_overlays.size();
	}

	@Override
	protected boolean onBalloonTap(int index, OverlayItem item) {
		if (diningLocs[index].type == DiningModel.DiningType.RESTAURANT) {
			
			if (diningLocs[index].menu.equalsIgnoreCase("")) {	//there is no website for this
				
				Toast.makeText(ctx, "No menu available for this dining location.", Toast.LENGTH_LONG).show();
				return true;
				
			} else {
			
				final Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(diningLocs[index].menu));
				ctx.startActivity(intent);
				return true;
				
			}

		} else if (diningLocs[index].type == DiningModel.DiningType.DINING_HALL) {
			Intent i = new Intent(ctx, DiningDisplay.class);
			i.putExtra("name", diningLocs[index].name);
			ctx.startActivity(i);
			return true;
		
		} else 
			return false;   //ERROR
	}

}
